package com.huibao.hulibao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.huibao.hulibao.R;
import com.huibao.hulibao.adapter.HuliSelectedtLvAdapter;
import com.huibao.hulibao.entity.HuLiEntity;
import com.huibao.hulibao.utils.T;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lizhaoyong on 2018/5/17.
 * 护理数据选择界面
 */
public class StartHuliSelectedActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_finish)
    TextView tv_finish;
    @BindView(R.id.lv_start_huli_selected)
    ListView lv_start_huli_selected;


    @OnClick(R.id.iv_back)
    public void back() {
        onBackPressed();
    }

    // 返回码
    private int requestCode = 1;

    private HuLiEntity.HuLiDetailData hdd;

    // 拆分字符串的后存放的集合
    private ArrayList<String> al_datas;
    // 用户选中数据后存储数据
    private ArrayList<String> al_selected;

    // 上个页面的下标索引
    private String position;

    // 是否是单选的标识
    private boolean is_single = false;


    @OnClick(R.id.tv_finish)
    public void BackToStartHuli() {
//        for (int i = 0; i < al_selected.size(); i++) {
//            LogUtils.d("data = " + al_selected.get(i));
//        }

        if (al_selected.size() > 0) {
            hdd.selected_str = "";
            StringBuffer sb = new StringBuffer(hdd.selected_str);
            if (al_selected.size() == 1) {
                sb.append(al_selected.get(0));
            } else {
                for (int i = 0; i < al_selected.size(); i++) {
                    if (i == (al_selected.size() - 1)) {
                        sb.append(al_selected.get(i));
                    } else {
                        sb.append(al_selected.get(i)).append(";");
                    }
                }
            }
            hdd.selected_str = sb.toString();
        }

        Intent intent = new Intent();
        intent.putExtra("back_data", hdd);
        intent.putExtra("position", position);
        setResult(requestCode, intent);
        finish();
    }


    // 添加和删除选中的数据
    public void saveSelectData(String str) {
        if (is_single) {
            al_selected.add(str);
            BackToStartHuli();
        } else {
            al_selected.add(str);
        }
    }

    public void deleteSelectedData(String str) {
        al_selected.remove(str);
    }


    @Override
    public void onBackPressed() {
        BackToStartHuli();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_huli_selected);
        ButterKnife.bind(this);
        al_datas = new ArrayList<String>();
        al_selected = new ArrayList<String>();


        hdd = (HuLiEntity.HuLiDetailData) getIntent().getSerializableExtra("selected_data");
        // 多选和单选的判断 rad单选 chb多选
        if (hdd.t.equals("rad")) {
            is_single = true;
        } else if (hdd.t.equals("chb")) {
            is_single = false;

        }


        position = getIntent().getStringExtra("position");
        tv_title.setText(hdd.n);
        // LogUtils.d("content = " + hdd.vi);
        if (hdd.vi.contains(",")) {
            String[] arr = hdd.vi.split(",");
            for (int i = 0; i < arr.length; i++) {
                al_datas.add(arr[i]);
            }
        } else {
            al_datas.add(hdd.vi);
        }
        lv_start_huli_selected.setAdapter(new HuliSelectedtLvAdapter(al_datas, getApplicationContext(), StartHuliSelectedActivity.this));
    }
}
